<?php
session_start();
//require_once("../config/include.inc.php");
require_once("../config/include_tokyo.inc.php");
require_once("../class/Core_Lib_Page.php");
require_once("../class/Common_Function.php");


//访问权限验证
$userId = $_SESSION['user_id'];
$userRole = $_SESSION['user_role'];
$current_system = $_SESSION['current_system'];
if($userRole == 1 || $userRole == 4 || $userRole == 5){
	//管理员,订单管理员,任务管理员+订单管理员
}else{
	if($userRole > 0){
		$url = "../index.php?loginError=2";//没权限
	}else{
		$url = "../index.php?loginError=3";//没登录
	}	
	echo "<script language='javascript' type='text/javascript'>";
	echo "window.location.href='$url'";
	echo "</script>";
}


//获取form值
$doFlag = $_POST["doFlag"];
if($doFlag == "addProcess"){
	//项目进展
	$projectId = $_POST["projectId"];
	$processStr = $_POST["process_".$projectId];
	$sql = "insert into projectprocess(project_id,project_process) values ($projectId,'$processStr')";
	$db->Execute($sql);
}
//end if doFlag


//分页设置
$list_rows = 50; //每页显示的记录数
if($doFlag == "toAllProject"){
	$sql_list = "SELECT COUNT(*) as total_rows from projects p where  p.project_owner=".$userId;
}else{
	$sql_list = "SELECT COUNT(*) as total_rows from projects p where p.project_status = 1 and p.project_owner=".$userId;
}
$result = $db->Execute($sql_list);
if (!empty($result)) {
	$totalRows = $result->fields['total_rows'];
}
$params = array(
	'total_rows'=>$totalRows,
	'list_rows'=>$list_rows,
	'method'    =>'ajax',
	'ajax_func_name' =>'goToPage',
	'now_page' => !empty($_POST['nowPage']) ? $_POST['nowPage'] : 1 
);
$page = new Core_Lib_Page($params);
$limit_m = $page->first_row;
$limit_n = $list_rows;



//显示列表
if($doFlag == "toAllProject"){
	$sql = "SELECT * from projects p  where  p.project_owner=".$userId." order by create_time desc limit $limit_m,$limit_n"; 
}else{
	$sql = "SELECT * from projects p  where p.project_status = 1 and p.project_owner=".$userId." order by project_level desc,create_time desc limit $limit_m,$limit_n"; 
}
$result = $db->Execute($sql);
?>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">

<!--Begin:head-->
<? $pageTitle = "项目列表"; ?>
<?php include("../head.php"); ?>
<!--End:head-->


<body>
<?php include("../topMenu.php"); ?>


<form name="listForm" action="../project/projectList.php" method="post">
<div id="container" >	


<div class="grayTable">	
<?php
$topTitle =  $topTitle."    <a href='javascript:toCurrentProject()'>当前项目</a>";
$topTitle =  $topTitle."    <a href='javascript:toAllProject()'>所有项目</a>";
$topTitle =  $topTitle."    <a href='projectInput.php'>新增项目</a>";
echo $topTitle;
?>
<table >
<thead>
	<tr>
		<th scope="col" >ID</th>
	    <th scope="col" >项目名称</th>		
	    <th scope="col" >项目周期</th>
		<th scope="col" >进展记录</th>	
		<th scope="col" >操作</th>	
	</tr>
</thead>

<tbody>	
<?php
	$commonFunction = new Common_Function();
	if(empty($result) || $result->RecordCount() < 1){
		echo "<tr><td colspan=5 align='center'>记录为空.</td></tr>";
	}else{
			while (!$result->EOF) {
				//取当前项目的进展记录
				$sql = "SELECT  * from projectprocess where project_id=".$result->fields['id']." order by modifytime desc limit 0, 5";	
				$resultProcess = $db->Execute($sql);
				$currentProcess = "";		
				while (!$resultProcess->EOF) {
					$project_processField = $resultProcess->fields['project_process'];
					if(strlen($project_processField)>100){
						$project_processField = substr($project_processField,0,100)."...";
					}
					$currentProcess = $resultProcess->fields['modifytime']."&nbsp;&nbsp;".$project_processField."<br>".$currentProcess;
					$resultProcess->MoveNext();	
				}
				$strTR = "<tr>";	
				$strTR = $strTR."<td>".$result->fields['id']."</td>";
				$strTR = $strTR."<td>".$result->fields['project_name']."</td>";
				$strTR = $strTR."<td>[".$commonFunction->getProjectStatusName($result->fields['project_status'])."]<br>开始时间:".$result->fields['begin_time']."<br>完成时间:".$result->fields['expect_end_time']."</td>";
				$strTR = $strTR."<td>".$currentProcess."<input type='text' size=30 name='process_".$result->fields['id']."' id='process_".$result->fields['id']."'><input type='button' value='提交' onclick='addProcess(".$result->fields['id'].")'></td>";
				$strTR = $strTR."<td><input type='button' value='编辑' onclick='doEdit(".$result->fields['id'].")'><br><input type='button' value='报告' onclick='doReport(".$result->fields['id'].")'></td>";
				$strTR = $strTR."</tr>";	
				echo $strTR;
				$result->MoveNext();	
			}
	}

?>

</tbody>
					
</table>	
<br>
<!--Page 分页 -->
<div id="page"><?php echo $page->show(2);?></div>



</div>



</div><!--end container -->

<input type="hidden" name="doFlag" id="doFlag" value="<?php echo $doFlag; ?>">
<input type="hidden" name="projectId" id="projectId" >
<input type="hidden" name="nowPage" id="nowPage" value="<?php echo $nowPage; ?>">
</form>


</body>




</html>

<script>

<?php
$doFlag = $_POST["doFlag"];
if($doFlag == "addProcess"){
	$projectId = $_POST["projectId"];
	echo "$('#process_".$projectId."').focus(); "; 
}
?>


/************项目进展**************************/
function addProcess(id){
	document.getElementById('doFlag').value = "addProcess";
	document.getElementById('projectId').value = id;
	var processStr = document.getElementById('process_'+id).value;
	if(processStr == ""){
		alert("进展内容不能为空")
		document.getElementById('process_'+id).focus();  
		return;
	}
	document.listForm.submit();
}


/************进行中的项目**************************/
function toCurrentProject(){
	window.location.href = "projectList.php"; 
}

/************所有项目**************************/
function toAllProject(){
	document.getElementById('doFlag').value = "toAllProject";
	document.listForm.submit();
}

/************分页**************************/
function goToPage(nowPage){
	document.getElementById('nowPage').value = nowPage;
	document.listForm.submit();
}

/************编辑项目**************************/
function doEdit(id){
	 window.location.href="projectInput.php?id="+id; 
}

/************项目报告**************************/
function doReport(id){
	 window.location.href="projectReport.php?id="+id; 
}

</script>

